<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-9-29
 * Time: 上午9:49
 */
class SaleorderModel extends BaseModel{
    private $_table;
    protected $_name = 'sale_order';
    protected $_isbug = false;


    /**
     * @param $where
     * @param $item_page
     * @param $offset
     * @param $order
     * @param $group
     * @return array
     */
    public function getSaleOrderListByWhere($where,$item_page,$offset,$order,$group){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $sql = $db->select ()->from ( $this->_name ,array("id as order_id","order_key","business_id","user_id","order_from","order_type","order_date","status","worker_id","order_amount","checked_time"))
            ->joinLeft("sale_order_sku","$this->_name.id = sale_order_sku.order_id" ,array("id as order_sku_id","delivery_area","delivery_time","delivery_method","service_id","currency_id","uom_id","num","price","amount","buy_sku_type","buy_sku_id"))
            ->joinLeft ( "sale_order_business", "sale_order_business.order_id = $this->_name.id", array(""))
            ->joinLeft ( "product", "sale_order_sku.product_id = product.id", array("product_name","product_code"))
            ->joinLeft ( "product_cates", "product.cate_id = product_cates.id", "cate_name" )
            ->joinLeft ( "product_brands", "product.brand_id = product_brands.id", "brand_name" )
            ->joinLeft ( "res_service", "res_service.id = sale_order_sku.service_id", "service_name" )
            ->joinLeft ( "res_currency", "res_currency.id = sale_order_sku.currency_id", array("name as currency_name","mark as currency_mark") )
            ->joinLeft ( "users_invoice as ui", "sale_order_business.buy_business_id = ui.id",array ("business_name","contacts","mobile","qq"))
            ->where ( $where )->__toString ();

        if($group){
            $sql.= " group  by ".$group;
        }
        if($order){
            $sql.= " order  by ".$order;
        }
        if($item_page){
            $sql.= " limit $offset,$item_page";
        }

        if ($this->_isbug == true) {
            echo __METHOD__ . ": " . $sql . "<hr/>";
        }
        return $db->fetchAll( $sql );
    }

    /**
     * @param $where
     * @return string
     */
    public function getSaleOrderCountByWhere($where){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $sql = $db->select ()->from ( $this->_name ,"count(*)")
            ->joinLeft("sale_order_sku","$this->_name.id = sale_order_sku.order_id" ,"")
            ->joinLeft ( "sale_order_business", "sale_order_business.order_id = $this->_name.id", array(""))
            ->joinLeft ( "product", "sale_order_sku.product_id = product.id", "")
            ->joinLeft ( "product_cates", "product.cate_id = product_cates.id", "")
            ->joinLeft ( "product_brands", "product.brand_id = product_brands.id", "" )
            ->joinLeft ( "res_service", "res_service.id = sale_order_sku.service_id", "" )
            ->joinLeft ( "res_currency", "res_currency.id = sale_order_sku.currency_id", "")
            ->joinLeft ( "users_invoice as ui", "sale_order_business.buy_business_id = ui.id","")
            ->where ( $where )->__toString ();
        if ($this->_isbug == true) {
            echo __METHOD__ . ": " . $sql . "<hr/>";
        }
        return $db->fetchOne( $sql );
    }

    /**
     * @param $where
     * @param $item_page
     * @param $offset
     * @param $order
     * @param $group
     * @return array
     */
    public function getBuyOrderListByWhere($where,$item_page,$offset,$order,$group){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $sql = $db->select ()->from ( $this->_name ,array("id as order_id","order_key","business_id","user_id","order_from","order_type","order_date","status","worker_id","order_amount","checked_time"))
            ->joinLeft("sale_order_sku","$this->_name.id = sale_order_sku.order_id" ,array("id as order_sku_id","delivery_area","delivery_time","delivery_method","service_id","currency_id","uom_id","num","price","amount","buy_sku_type","buy_sku_id"))
            ->joinLeft ( "sale_order_business", "sale_order_business.order_id = $this->_name.id", array("sale_business_name","sale_contacts","sale_mobile","sale_qq"))
            ->joinLeft ( "product", "sale_order_sku.product_id = product.id", array("product_name","product_code"))
            ->joinLeft ( "product_cates", "product.cate_id = product_cates.id", "cate_name" )
            ->joinLeft ( "product_brands", "product.brand_id = product_brands.id", "brand_name" )
            ->joinLeft ( "res_service", "res_service.id = sale_order_sku.service_id", "service_name" )
            ->joinLeft ( "res_currency", "res_currency.id = sale_order_sku.currency_id", array("name as currency_name","mark as currency_mark") )
            ->joinLeft ( "users_invoice as ui", "sale_order_business.sale_business_id = ui.id",array ("business_name","contacts","mobile","ifnull(qq,sale_order_business.sale_qq) as qq"))
            ->where ( $where )->__toString ();

        if($group){
            $sql.= " group  by ".$group;
        }
        if($order){
            $sql.= " order  by ".$order;
        }
        if($item_page){
            $sql.= " limit $offset,$item_page";
        }

        if ($this->_isbug == true) {
            echo __METHOD__ . ": " . $sql . "<hr/>";
        }
        return $db->fetchAll( $sql );
    }

    /**
     * @param $where
     * @return string
     */
    public function getBuyOrderCountByWhere($where){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $sql = $db->select ()->from ( $this->_name ,"count(*)")
            ->joinLeft("sale_order_sku","$this->_name.id = sale_order_sku.order_id" ,"")
            ->joinLeft ( "sale_order_business", "sale_order_business.order_id = $this->_name.id", array(""))
            ->joinLeft ( "product", "sale_order_sku.product_id = product.id", "")
            ->joinLeft ( "product_cates", "product.cate_id = product_cates.id", "")
            ->joinLeft ( "product_brands", "product.brand_id = product_brands.id", "" )
            ->joinLeft ( "res_service", "res_service.id = sale_order_sku.service_id", "" )
            ->joinLeft ( "res_currency", "res_currency.id = sale_order_sku.currency_id", "")
            ->joinLeft ( "users_invoice as ui", "sale_order_business.sale_business_id = ui.id","")
            ->where ( $where )->__toString ();
        if ($this->_isbug == true) {
            echo __METHOD__ . ": " . $sql . "<hr/>";
        }
        return $db->fetchOne( $sql );
    }

    /**
     * 详情
     * @param $where
     * @return array
     */
    public function getSaleOrderDetail($where){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $sql = $db->select ()->from ( $this->_name )->joinLeft("sale_order_sku","$this->_name.id = sale_order_sku.order_id" ,array("delivery_area","delivery_time","service_id","currency_id","uom_id","num","price","amount","buy_sku_type","buy_sku_id","delivery_method","delivery_addr"))
            ->joinLeft ( "iq_quotation", "iq_quotation.id = sale_order_sku.buy_sku_id", array("delivery_area"))
            ->joinLeft ( "product", "sale_order_sku.product_id = product.id", array("product_name","product_code"))
            ->joinLeft ( "product_cates", "product.cate_id = product_cates.id", "cate_name" )
            ->joinLeft ( "product_brands", "product.brand_id = product_brands.id", "brand_name" )
            ->joinLeft ( "users_invoice as uiiq", "iq_quotation.business_id = uiiq.id",array ( "uiiq.business_name","uiiq.contacts","uiiq.mobile","uiiq.qq"))
            ->joinLeft ( "users_invoice as uiii", "$this->_name.user_id = uiii.user_id",array ( "uiii.business_name as ibusiness_name","uiii.contacts as icontacts","uiii.mobile as imobile","uiii.qq as iqq"))
            ->where ( $where );

        if ($this->_isbug == true) {
            echo __METHOD__ . ": " . $sql . "<hr/>";
        }
        return $db->fetchRow( $sql );
    }

##############################################################
    /**
     * 订单成功，订单信息
     * @param $where
     * @return array
     */
    public function getsaleorder($where){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $sql = $db->select ()->from ( $this->_name )
            ->joinLeft("sale_order_sku","$this->_name.id = sale_order_sku.order_id" ,"")
            ->joinLeft ( "iq_quotation as iq", "sale_order_sku.buy_sku_id = iq.id","")
            ->joinLeft ( "users_invoice as ui", "ui.id = iq.business_id",array("business_name","contacts","mobile","qq"))
            ->where ( $where );
        if ($this->_isbug == true) {
            echo __METHOD__ . ": " . $sql . "<hr/>";
        }
        return $db->fetchRow( $sql );
    }

    public function getOrderByQuoteId($id){
        $this->_table = new self();
        $db = $this->_table->getAdapter();
        $select = $db->select();
        $select->from("sale_order_sku","order_id")
            ->where("buy_sku_id = '{$id}'");
        //todo 方便调试开启输出SQL语句
        $sql = $select->__toString();
        if($this->_isbug == true){
            echo $sql;
        }
        $result = $db->fetchOne($sql);
        return $result;
    }

    public function getOrderDetailInfo($where,$type="r"){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $sql = $db->select ()->from ( $this->_name )
            ->joinLeft("sale_order_sku","$this->_name.id = sale_order_sku.order_id" ,array("num","price","amount","delivery_area","delivery_time","delivery_method","delivery_addr"))
            ->joinLeft("sale_order_business","$this->_name.id = sale_order_business.order_id" ,array("buy_business_name","buy_contacts","buy_mobile","sale_business_name","sale_contacts","sale_mobile"))
            ->joinLeft ( "iq_quotation as iq", "sale_order_sku.buy_sku_id = iq.id","")
            ->joinLeft ( "product", "sale_order_sku.product_id = product.id", array("product_name","product_code"))
            ->joinLeft ( "product_cates", "product.cate_id = product_cates.id", "cate_name" )
            ->joinLeft ( "product_brands", "product.brand_id = product_brands.id", "brand_name" )
            ->joinLeft ( "res_service", "res_service.id = sale_order_sku.service_id", "service_name" )
            ->joinLeft ( "res_currency", "res_currency.id = sale_order_sku.currency_id", array("name as currency_name","mark as currency_mark","unit"))
            ->joinLeft ( "users_invoice as ui", "ui.id = iq.business_id",array("business_name","contacts","mobile","qq"))
            ->where ( $where );
        if ($this->_isbug == true) {
            echo __METHOD__ . ": " . $sql . "<hr/>";
        }
        if($type=='r'){
            return $db->fetchRow( $sql );
        }else{
            return $db->fetchAll( $sql );
        }

    }

}